var formValue = {
	Dashboard:"Dashboard",
	eupDpartmntId:"departmentId",
	eupDpartmntDesc:"departmentDesc",
	eupBusnUCode:"busnUCode",
	eupBsnUCDesc:"busnUCodeDesc",
	eupAmexUCd:"amexUCode",
	eupAmexUDesc:"amexUDesc",
	eupExpEndCd:"excpEndCode",
	radio:"detailAllAccess",
	eupPhone:"1234567890",
	eupEmail:"test@gmail.com",
	eupMFN:"firstname",
	eupMI:"mIn",
	eupLN:"lastname",
	eupMEmail:"manager@gmail.com",
	eupVPName:"VPName",
	eupReason:"reason"
};


/*********************************
 * Edit User Profile
 * ******************************/
uam.eup = uam.eup || {};

uam.eup.loadUserProfile = function (userid){
	
	var params = {
		'taskId' : 'N001',
		'TaskSubID' : 'GetUserProfile',
		'userid' : userid
	};
	uam.Utils.callTask(params, function(responseData) {
		
		eval("var responseContext=" + responseData);
		
		if(responseContext["status"] == uam.Utils.responseCode.OK){
			
			var data = responseContext["data"];
			var userProfile = uam.Utils.getObject(data.userProfile);
			var reasonList = uam.Utils.getObjectList(data.optBlueList);
			var funcGroupList = uam.Utils.getObjectList(data.functionalGroups);
			var funcGroupRoleList = uam.Utils.getObjectList(data.funcGRPRole);

			for (var n in userProfile) {
				uam.eup.assignValue(n, userProfile[n]);
			}
			
			// Initial Secondary Functional Group/Role

			var grpName = null;// current user's group name
			var grpID = null;// current user's group id
			var roleList = [];
			
			for ( var i = 0; i < funcGroupRoleList.length; i++ ) {
				// {FUNC_GRP_ID: "1", FUNC_GRP_ROLE_ID: "7", FUNC_GRP_ROLE_NM: "Regional & Local Support"}
				if (funcGroupRoleList[i]["FUNC_GRP_ROLE_ID"] == userProfile["FUNC_GRP_ROLE_ID"]) {
					grpID = funcGroupRoleList[i]["FUNC_GRP_ID"];
				}
			}
			
			for ( var i = 0; i < funcGroupList.length; i++ ) {
				// {radioName: "OptBlue", radio: "8"} 
				if (funcGroupList[i].radio == grpID) {
					grpName = funcGroupList[i].radioName;
					grpID = funcGroupList[i].radio;
				}
			}
			if (grpName != null) {
				
				for ( var i = 0; i < funcGroupRoleList.length; i++ ) {
					// {FUNC_GRP_ID: "1", FUNC_GRP_ROLE_ID: "7", FUNC_GRP_ROLE_NM: "Regional & Local Support"}
					if (funcGroupRoleList[i]["FUNC_GRP_ID"] == grpID) {
						roleList.push(funcGroupRoleList[i]);
					}
				}
				
				uam.eup.initSecFuncGrpAndRoleContainer(grpName, roleList, userProfile["FUNC_GRP_ROLE_ID"]);
				uam.eup.initSubSecFuncContainer(grpName, reasonList);
			}
			
			
			uam.eup.initFuncGrpRoleContainer(funcGroupList, funcGroupRoleList, userProfile["FUNC_GRP_ROLE_ID"]);

		}
	});
}

uam.eup.initProjects = function(userId) {
	uam.Utils.callTask({taskId:"N001", TaskSubID: "GetAllProj" }, function(data) {
		//console.log(data);
		eval("responseContent = " + data);
		if (responseContent.status == uam.Utils.responseCode.OK) {
			var projData = responseContent.data;
			
			$("#srcList").empty();
			for (var i = 0; i < projData.length; i++) {
				// [{  "MS_PROJ_ID": 1, "MS_PROJ_NM": "MARC", "CREAT_TS": "2009-06-03 05:58:24.203" }]
				$("<option ondblclick='addSrcToDestList()'/>")
				.val(projData[i]["MS_PROJ_ID"])
				.append(uam.Utils.trim(projData[i]["MS_PROJ_NM"], ""))
				.appendTo("#srcList");
			}
		}
		
	});
	uam.Utils.callTask({taskId:"N001", TaskSubID: "GetUserProj", userId: userId }, function(data) {
		eval("responseContent = " + data);
		if (responseContent.status == uam.Utils.responseCode.OK) {
			var uProjData = responseContent.data;
			$("#destList").empty();
			for (var i = 0; i < uProjData.length; i++) {
				// [{ "MS_PROJ_ID": 1, "MS_PROJ_NM": "MS_PROJ_NM" }]
				$("<option ondblclick='deleteFromDestList()'/>")
				.val(uProjData[i]["MS_PROJ_ID"])
				.append(uam.Utils.trim(uProjData[i]["MS_PROJ_NM"], ""))
				.attr("selected", true)
				.appendTo("#destList");
			}
		}
		
	});
	
}

// id : function(value)
uam.eup.assignMapper = {
		"checkbox" : "uam.eup.assignCheckbox",
		"radio3" : "uam.eup.assignRadio3",
		"eupDpartmntDesc" : "uam.eup.assignEupDpartmntDesc"
};


uam.eup.assignValue = function(id, value) {
	// console.log("id="+id+", value="+value);
	if (uam.eup.assignMapper[id]) {
		// special
		//console.log("special: " + uam.eup.assignMapper[id]);
		eval(uam.eup.assignMapper[id]+'(value)');
		
	} else {
		// general
		if ($("#" + id) && value) {
			$("#" + id).val(value);
		}
	}
}
uam.eup.assignEupDpartmntDesc = function(value){
	$("#eupDeptDescDynamic").val(value);
	$("#eupDpartmntDesc").val(value);
}
// LIF Governance
uam.eup.assignRadio3 = function(value) {
	$.each($(":radio[name=radio3]"), function(index, element){
		//console.log("index: " + index + ", value: "+ element.value);
		if (element.value == value) {
			element.checked = true;
		}
	});
}
uam.eup.assignCheckbox = function(value) {
	if (value == 'Y')
		$("#checkbox").attr("checked", true);
}

uam.eup.initSecFuncGrpAndRoleContainer = function(grpName, roleList, userRoleID) {
	var container = $("#secFuncGrpAndRoleContainer");
	container.empty();
	container.append("<div style='font-weight:bold;'>" + grpName +"</div>");
	
	for ( var i = 0; i < roleList.length; i++ ) {
		// {FUNC_GRP_ID: "1", FUNC_GRP_ROLE_ID: "7", FUNC_GRP_ROLE_NM: "Regional & Local Support"} 
		var role = roleList[i];
		var roleEle = $("<input type='radio' name='eupGrpRole'/>");
		var id = "eupGrpRole_" + i;
		
		roleEle.attr("id", id).val(role["FUNC_GRP_ROLE_ID"]).change(uam.eup.showSubSecFuncContainer);
		
		var label = $("<label style='font-weight: normal;'></label>").attr("for", id).append(role["FUNC_GRP_ROLE_NM"]);
		
		if (userRoleID == role["FUNC_GRP_ROLE_ID"]) {
			label.css("color", "blue");
		}
		
		container.append(roleEle);
		container.append(label);
		
		if(i < roleList.length-1) {
			container.append("<br>");
		}
	}
}

uam.eup.showSubSecFuncContainer = function() {
	$('#subSecFuncContainer').show();
}
// Save the checked access reason id
uam.eup.reasonList = {};
uam.eup.reasonChange = function(event){
	uam.eup.reasonList[this.value] = this.checked;
}
uam.eup.initSubSecFuncContainer = function(grpName, reasonList) {
	
	$("#subSecFuncContainer_reasonTitle").text("Select the reason for " + grpName + " access:");
	var reasonOther = null;
	
	for (var i = 0; i < reasonList.length; i++) {
		var reason = reasonList[i];
		var reasonID = reason["OPT_BLUE_ACS_RSN_CD"];
		var reasonDesc = reason["OPT_BLUE_ACS_RSN_DS"];
		
		if (typeof(reasonDesc) == 'object') {// Other reason
			
			$("#subSecFuncContainer_reasonOther")
			
			.append("<input type='hidden' value='" + reasonID + "' name='reasonOtherID'></input>")
			.append("<input type='checkbox' id='reasonOther' name='reasonOther' />")
			.append("<label for='reasonOther' style='font-weight: normal;'>Other</label>")
			.append("<input type='text' id='reasonOtherContent' name='reasonOtherContent' />")
			.append("<div class='status' style='display:inline-block;'></div>");
		} else {
			$("<input type='checkbox' />").attr("id", "reason_" + i).val(reasonID).change(uam.eup.reasonChange).appendTo("#subSecFuncContainer_reasons");
			$("#subSecFuncContainer_reasons").append("<label for=reason_" + i + " style='font-weight: normal;'>" + reasonDesc + "</label>");
			
		}
	}
}

uam.eup.initFuncGrpRoleContainer = function(funcGroupList, roleList, roleID) {
	//console.log(funcGroupList);
	//console.log(funcGroupRoleList);
	
	var container = $("#funcGrpRoleContainer").empty();
	for ( var i = 0; i < funcGroupList.length; i++ ) {
		// group = {radioName: "OptBlue", radio: "8"} 
		var group = funcGroupList[i]; 
		container.append("<div class='funcGroupLabel'>* " + group.radioName + "</div>");
		for ( var j = 0; j < roleList.length; j++ ) {
			// {FUNC_GRP_ID: "1", FUNC_GRP_ROLE_ID: "7", FUNC_GRP_ROLE_NM: "Regional & Local Support"} 
			var role = roleList[j]; 
			if(role.FUNC_GRP_ID == group.radio) {
				if (roleID == role.FUNC_GRP_ROLE_ID) {
					container.append("<input type='checkbox' name='cekBoxGrp2' value='"+role.FUNC_GRP_ROLE_ID+"' onclick='return false;'/><font style='color:blue;'>"+role.FUNC_GRP_ROLE_NM+"</font><br>");
				} else {
					container.append("<input type='checkbox' name='cekBoxGrp2' value='"+role.FUNC_GRP_ROLE_ID+"' onclick='return false;' />"+role.FUNC_GRP_ROLE_NM+"<br>");
				}
					
			}
		}
	}
	
}

uam.eup.loadSecurityData = function() {
	var params = {
		'taskId' : 'N001',
		'TaskSubID' : 'GetSecurityData'
	};
	uam.Utils.callTask(params, function(responseData) {
		
		eval("var responseContext=" + responseData);
		//console.log("** Get Security Data:");
		//console.log(responseContext);
		if (responseContext["status"] == uam.Utils.responseCode.OK) {
			uam.eup.initScyGrp (responseContext.data);
		}
	});
				
}


uam.eup.initScyGrp = function(scyGrpData) {
	
	$("#securityGroup").empty().append("<UL/>");
	uam.eup.createScyGrp($("#securityGroup"), scyGrpData["MARC"], "scyGrpMarc");
	uam.eup.createScyGrp($("#securityGroup"), scyGrpData["ICARD"], "scyGrpIcard");
	uam.eup.createScyGrp($("#securityGroup"), scyGrpData["DASHBOARD"], "scyGrpDashboard");
}
// call by Task 
uam.eup.createScyGrp = function (parentElement, securityGroup, name) {
console.log(securityGroup);
	if ( !parentElement || !securityGroup ) {
		return;
	}
	if (securityGroup instanceof Array) {
		
		var element = ($(parentElement).is('UL'))
						? parentElement
						: $("<UL/>").appendTo(parentElement);// <ul/>
		
		for (var i = 0; i < securityGroup.length; i++) {
			uam.eup.createScyGrp(element, securityGroup[i], name);
		}
		
	} else {
		
		var element = $("<LI/>")
			.append(securityGroup.groupName)
			.appendTo(parentElement);
		
		if (securityGroup.children && securityGroup.children.length > 0) {
			uam.eup.createScyGrp(element, securityGroup.children, name);
		} else {
			$("<input type='radio' name='"+name+"'>").prependTo(element);
		}
	}
	
}

  /*
  {
    "groupID": "D09773F94699124B4D75B48F1B358327",
    "groupName": "3rd Party Users",
    "isGroup": true,
    "children": [
    ]
  }
  
  	<div class="securityGroup" id="securityGroup">
	    <ul>
	    	<li><input type="checkbox" readonly="readonly">xxxxx</li>
	    	<li><input type="checkbox" readonly="readonly">xxxxx
	    		<ul>
	    			<li><input type="checkbox" readonly="readonly">xxxxx
	    		</ul>
	    	</li>
	    	<li><input type="checkbox" readonly="readonly">xxxxx</li>
	    </ul>
	</div>
  */
uam.eup.createSecurityGroup = function (parentElement, securityGroup) {

	if ( !parentElement || !securityGroup ) {
		return;
	}
	if (securityGroup instanceof Array) {
		//console.log("++Array: " + securityGroup.length);
		//console.log(parentElement[0].tagName);
		//console.log($(parentElement).is('UL'));
		
		var element = ($(parentElement).is('UL'))
						? parentElement
						: $("<UL/>").appendTo(parentElement);// <ul/>
		
		for (var i = 0; i < securityGroup.length; i++) {
			uam.eup.createSecurityGroup(element, securityGroup[i]);
		}
		
	} else {
		
		if (securityGroup.isGroup == true) {
			
			//console.log("**Group: " + securityGroup.groupID + ", " + securityGroup.groupName);
			
			var element = $("<LI/>")
			.append(securityGroup.groupName)
			.appendTo(parentElement);
			
			uam.eup.createSecurityGroup(element, securityGroup.children);
		} else  {
			
			//console.log("  --Role: " + securityGroup.groupID + ", " + securityGroup.groupName);
			
			var element = $("<LI/>")
			.append("<input type='checkbox' onclick='return false'>")
			.append(securityGroup.groupName)
			.appendTo(parentElement);
			
		}
	}
	
}

uam.eup.initClmId = function(userId) {
	 
	uam.Utils.callTask({taskId:"N001", TaskSubID: "GetCLMID", userId: userId}, function(responseData) {
		console.log(responseData);
		eval("var responseContent=" + responseData);
		if (responseContent.status == uam.Utils.responseCode.OK) {
			// console.log(responseContent.data);
			$("#eupClmID").val(responseContent.data);
		}
		
	});
}


/**
 * window.onload
 */
$(document).ready(function(){

	// XXX assign userid value
	//get user id from cookie
	var userid = '8165840';
	uam.eup.loadUserProfile(userid);
	uam.eup.loadSecurityData();
	uam.eup.initProjects(userid);
	uam.eup.initClmId(userid);
	
	$("#eupEmpContID").val(userid);
	$("#statusDate").val(uam.Utils.date2String(new Date()));
	
});